package br.com.hsp.system.beans;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;

import javax.el.ELException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.servlet.http.HttpServletRequest;

import br.com.hsp.system.repository.CartaoRepository;
import br.com.hsp.system.repository.PacienteRepository;
import br.com.hsp.users.MedicoClinica;
import br.com.hsp.users.Paciente;

@ManagedBean
// @ViewScoped
public class PacienteBean {
	
	@ManagedProperty(value="#{userSessionBean}")
	private UserSessionBean session;

	private Paciente paciente = new Paciente();
	
	private String codCartao;
	
	private List<Paciente> pacientes;

	public void add() {
		
		EntityManager manager = this.getManager();
		PacienteRepository pacienteRepository = new PacienteRepository(manager);
		try {
			if (!this.codCartao.isEmpty()) {
				CartaoRepository cartaoRepository = new CartaoRepository(
						manager);
				this.paciente.setCartao(cartaoRepository
						.procura(this.codCartao));
			}
			if (this.paciente.getId() == null) {
				pacienteRepository.add(this.paciente);
			} else {
				pacienteRepository.atualiza(this.paciente);
			}
		} catch (PersistenceException ex) {
			System.out.println("PersistenceException == " + ex);
		}
		this.paciente = new Paciente();
		this.codCartao = null;
	}

	public void preparaAlteracao() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		PacienteRepository repository = new PacienteRepository(manager);
		this.paciente = repository.procura(id);
		if (this.paciente.getCartao() != null) {
			this.codCartao = this.paciente.getCartao().getCodigo_cartao();
		}

	}

	public void preparaRemove() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		paciente.setId(Long.parseLong(params.get("id")));
		System.out.println(paciente.getId());
	}

	public void limpaRemove() {
		this.paciente = new Paciente();
	}

	public void remove() {
		System.out.println(paciente.getId());
		EntityManager manager = this.getManager();
		PacienteRepository repository = new PacienteRepository(manager);
		repository.remove(paciente.getId());
		this.paciente = null;
		this.pacientes = null;
	}

	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}

	public List<Paciente> getPacientes() {
		if (this.session.getUserSession() != null && this.session.getUserSession().getClass().toString().contains("MedicoClinica")) {
			EntityManager manager = this.getManager();
			PacienteRepository repository = new PacienteRepository(manager);
			 this.pacientes = repository.getListaParaMedico(session.getUserSession().getId());
		} else {
			if (this.pacientes == null) {
				EntityManager manager = this.getManager();
				PacienteRepository repository = new PacienteRepository(manager);
				this.pacientes = repository.getLista();
			}
		}
		return this.pacientes;
	}

	public Paciente getPaciente() {
		return paciente;
	}

	public void setPaciente(Paciente paciente) {
		this.paciente = paciente;
	}

	public String getCodCartao() {
		return codCartao;
	}

	public void setCodCartao(String codCartao) {
		this.codCartao = codCartao;
	}

	public UserSessionBean getSession() {
		return session;
	}

	public void setSession(UserSessionBean session) {
		this.session = session;
	}
	
}
